package org.gov.vis.master.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.gov.vis.components.VISHyperLink;
import org.gov.vis.gui.VISDataGrid;
import org.gov.vis.main.Main;
import org.gov.vis.master.controller.GopalaMithraCtlr;
import org.gov.vis.master.controller.GopalaMithraCtlrImpl;
import org.gov.vis.master.dto.GopalaMithraDto;
import org.gov.vis.master.gui.EditGopalaMithraDialog;
import org.gov.vis.master.gui.ViewGopalaMithraDialog;
import org.gov.vis.util.Util;

public class ViewGopalaMithraListener implements ActionListener,BaseListener{

	
	private GopalaMithraCtlr gopalaMithraCtlr = new GopalaMithraCtlrImpl();
	private ViewGopalaMithraDialog viewGopalaMithraDialog;
	private GopalaMithraDto gopalaMithraDto;
	
	final private ResourceBundle rb = Util.getResources();
	private EditGopalaMithraDialog editGopalaMithraDialog;


	public ViewGopalaMithraListener(ViewGopalaMithraDialog viewGopalaMithraDialog) {
		// TODO Auto-generated constructor stub
		this.viewGopalaMithraDialog = viewGopalaMithraDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		// TODO Auto-generated method stub
		ViewGopalaMithraDialog _viewGopalaMithraDialog =(ViewGopalaMithraDialog)_guiObject;
		gopalaMithraDto = new GopalaMithraDto();
		
		String code = _viewGopalaMithraDialog.getCodeTxtBx().getText();
		if(code!=null){
		 gopalaMithraDto.setCode(code);
		}
		/*otherDto.setNameOfOrg(_othersDialog.getTxtFldOrg().getText());
		otherDto.setNameOfAIWorker(_othersDialog.getTxtFldAIWorker().getText());
		otherDto.setHeadQuarter(_othersDialog.getTxtFldHeadQuarter().getText());
		otherDto.setJuridiction(_othersDialog.getTxtFldJurisdiction().getText());
		otherDto.setMandal(_othersDialog.getTxtFldMandal().getText());
		otherDto.setDistrict(_othersDialog.getTxtFldDistrict().getText());*/
	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub
		gopalaMithraDto = (GopalaMithraDto)_dtoObject;
		if(editGopalaMithraDialog!=null){
		editGopalaMithraDialog.getTxtbxCode().setText(gopalaMithraDto.getCode());
		editGopalaMithraDialog.getTxtbxName().setText(gopalaMithraDto.getName());
		editGopalaMithraDialog.getFatherTxtBx().setText(gopalaMithraDto.getFatherName());
		editGopalaMithraDialog.getJurisdiction().setText(gopalaMithraDto.getJurisdiction());
		editGopalaMithraDialog.getHeadQuarterTxtBx().setText(gopalaMithraDto.getHeadQuarter());
		editGopalaMithraDialog.getMandalTxtBx().setText(gopalaMithraDto.getMandal());
		editGopalaMithraDialog.getDistrictTxtBx().setText(gopalaMithraDto.getDistrict());
		editGopalaMithraDialog.getDtChoDateOfBirth().setDate(gopalaMithraDto.getDob());
		editGopalaMithraDialog.getNativeVillageTxtBx().setText(gopalaMithraDto.getNativeVillage());
		editGopalaMithraDialog.getJudMandalTxtBx().setText(gopalaMithraDto.getHqMandal());
		editGopalaMithraDialog.setHidnObject(gopalaMithraDto.getGplMthId());
		editGopalaMithraDialog.getJTextField1().setText(gopalaMithraDto.getHqDistrict());
		}
	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub
		
		
		if(actionEvent.getActionCommand().equals("Search")){
			setGuiDetailsToDto(viewGopalaMithraDialog);
			try {
				
			List<GopalaMithraDto> gopalaMithraLst = gopalaMithraCtlr.searchGopalaMithras(gopalaMithraDto);
			
			List<String> colLst = new ArrayList<String>();
			colLst.add("Code");
			colLst.add("Name");
			colLst.add("Village");
			colLst.add("Mandal");
			colLst.add("District");
			
			ArrayList<Object> dataList = new ArrayList<Object>(1);
			if(gopalaMithraLst != null && !gopalaMithraLst.isEmpty()){
				for(GopalaMithraDto dto:gopalaMithraLst){
					ArrayList<Object> lst = new ArrayList<Object>(1);
					//setting each property of the current Accessory object (row) to the list
					lst.add(dto);
					lst.add(dto.getCode());
					lst.add(dto.getName());
					lst.add(dto.getNativeVillage());
					lst.add(dto.getMandal());
					lst.add(dto.getDistrict());
					dataList.add(lst);
				}//end for 
			}//end if
			VISDataGrid.createDataPanel(dataList, colLst, viewGopalaMithraDialog.getPnlGopalaMithraResults(),this);
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		if(actionEvent.getSource() instanceof VISHyperLink){
			actionEvent.getSource();
			VISHyperLink visHyperLink = (VISHyperLink)actionEvent.getSource();
			GopalaMithraDto gopalaMithraDto = (GopalaMithraDto)visHyperLink.getObject();
			//System.out.println("action from the object----"+gopalaMithraDto.getCode());
			editGopalaMithraDialog = new EditGopalaMithraDialog(Main.getMainFrame(),
                    rb.getString("dialog_edit_gopalamithra_title"),
                    false);
			setDtoDetailsToGui(gopalaMithraDto);
			viewGopalaMithraDialog.dispose();
			

		}
		
	}



}
